package com.nbjtjc.safe.api.sys;

import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.MyPageInfo;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.sys.SysOptionGroupMapper;
import com.nbjtjc.safe.mapper.sys.SysOptionMapper;
import com.nbjtjc.safe.model.sys.SysGroup;
import com.nbjtjc.safe.model.sys.SysOptionGroup;
import com.nbjtjc.safe.service.sys.SysGroupService;
import com.nbjtjc.safe.service.sys.SysOptionGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 选项组
 */
@RequestMapping("/sys/group")
@CrossOrigin("*")
@RestController
public class SysGroupController {

    @Autowired
    private SysGroupService groupService;
    @Autowired
    private SysOptionGroupService groupOptionService;

    @Autowired
    private SysOptionGroupMapper optionGroupMapper;
    @Autowired
    private SysOptionMapper optionMapper;

    @RequestMapping(value = "/find", method = {RequestMethod.POST, RequestMethod.GET})
    public PageInfo<SysGroup> find(Page page,SysGroup sysGroup) {
        MyPageInfo<SysGroup> myPageInfo = null;
        try {
            myPageInfo = groupService.find(page,sysGroup);
            return myPageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            myPageInfo = new MyPageInfo<>();
            myPageInfo.setCode(400);
            myPageInfo.setMsg("查找失败，错误信息:" + e.getMessage());
        }
        return myPageInfo;
    }

    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    public Result update(SysGroup sysGroup) {
        Result result = new Result<>();
        try {
            result = groupService.update(sysGroup);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("创建失败：" + e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public Result add(SysGroup sysGroup) {
        Result result = new Result<>();
        try {
            result = groupService.add(sysGroup);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("创建失败：" + e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "delete", method = {RequestMethod.POST})
    public Result<Boolean> delete(SysGroup sysGroup) {
        Result<Boolean> result = new Result<>();
        try {
            result = groupService.delete(sysGroup);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("删除失败，错误信息：" + e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/findOptionGroup", method = {RequestMethod.POST})
    public Result optionSelect(String groupName) {
        Result result=new Result();
        try {
            result.setData(groupOptionService.find(groupName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @RequestMapping(value = "/addOptionGroup", method = {RequestMethod.POST})
    public Result<Boolean> addOptionGroup(SysGroup sysGroup,@RequestParam("ids") List<Integer> ids) {
        Result<Boolean> result = new Result<>();
        try {
            result = groupOptionService.addOptionGroup(sysGroup,ids);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("保存失败");
        }
        return result;
    }
    @RequestMapping(value = "/updateOptionGroup", method = {RequestMethod.POST})
    public Result<Boolean> updateOptionGroup(SysOptionGroup sysOptionGroup) {
        Result<Boolean> result = new Result<>();
        try {
            result = groupOptionService.update(sysOptionGroup);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("保存失败");
        }
        return result;
    }
    @RequestMapping(value = "/deleteOptionGroup", method = {RequestMethod.POST})
    public Result<Boolean> deleteOptionGroup(int optionGroupId) {
        Result<Boolean> result = new Result<>();
        try {
            int res = optionGroupMapper.deleteByPrimaryKey(optionGroupId);
            if(res==0){
                result.setFalse("删除失败，可能不存在这个选项");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse("保存失败");
        }
        return result;
    }

}
